package com.hc.swingHtml;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class MapFileUtil {
	public static void clearMapFile(String path){
		File file = new File(path);
		File[] fs = file.listFiles();
		for(File f : fs){
			f.delete();
		}
	}
	
	public static void checkFileExist(String path){
		File file = new File(path);
		if(!file.exists()){
			file.mkdir();
		}else{
			clearMapFile(path);
		}
	}
	
	public static String getMapPathById(int mapId){
		String fileName = "";
		String pFile = "img/bk/"+mapId;
		File file = new File(pFile);
		File[] fs = file.listFiles();
		for(File f : fs){
			if(checkIsImg(pFile + File.separator + f.getName())){
				fileName = f.getName();
			}
		}
		return "img/bk/"+mapId+"/"+fileName;
	}
	
	public static boolean checkIsImg(String name){
		try {
			BufferedImage bi = ImageIO.read(new File(name));
			if(bi == null){
				return false;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return true;
	}
}
